package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class roidController {
    @Reference
    private RoleService roleService;

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds){
        log.debug("CheckGroupController-add: CheckGroup{ " + role + " }");
        log.debug("CheckGroupController-add: checkitemIds{ " + Arrays.toString(permissionIds) + " }");
        roleService.add(role,permissionIds);
        log.debug(MessageConst.XIN_ZENG_JSE);
        return new Result(true, MessageConst.XIN_ZENG_JSE);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        log.debug("CheckGroupController-findPage: QueryPageBean{ " + pageBean + " }");
        PageResult pageResult = roleService.findPage(pageBean);
        log.debug("CheckGroupController-findPage: PageResult {" + pageResult + " }");
        return pageResult;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("CheckGroupController-findById: id{ " + id + " }");
        Role role = roleService.findById(id);
        log.debug("CheckGroupController-findById: checkGroup{ " +role + " }");
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS,role);
    }

    //根据检查组id查询关联的检查项id
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        log.debug("findPermissionIdsByRoleId: id: { " + id + " }");
        List<Integer> list = roleService.findPermissionIdsByRoleId(id);
        log.debug("checkItemIdList: { " + list + " }");
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,list);
    }

    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        log.debug("findMenuIdsByRoleId: id: { " + id + " }");
        List<Integer> list = roleService.findMenuIdsByRoleId(id);
        log.debug("checkItemIdList: { " + list + " }");
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,list);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds){
        log.debug("CheckGroupController-edit: CheckGroup{ " + role + " }");
        log.debug("CheckGroupController-edit: checkitemIds{ " + Arrays.toString(permissionIds) + " }");
        roleService.edit(role,permissionIds);
        log.debug(MessageConst.BIANJI_ROLT);
        return new Result(true, MessageConst.BIANJI_ROLT);
    }
    @RequestMapping("/delete")
    public Result delById(Integer id) {
        log.debug("delById : { " + id + "}");
        roleService.delById(id);
        log.debug(MessageConst.DELETE_CHECKGROUP_SUCCESS);
        return new Result(true, MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }
}
